package com.shop.cereshop.commons.enums;

import lombok.Getter;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @description: 钱包流水-交易类型枚举
 * @author zdd
 * @date 2025/9/20 10:27
 * @version 1.0
 */
@Getter
public enum TransactionTypeEnum {

    RECHARGE(1, "充值"),
    CONSUME(2, "消费"),
    REFUND(3,  "退款"),
    WITHDRAW(4, "提现"),


    RED_PACKET_PAY(5, "发红包付款"),
    RED_PACKET_RECEIVE(6, "领取红包收款"),
    RED_PACKET_REFUND(7, "红包退款"),

    TRANSFER_ORDER_PAY(8, "转账付款"),
    TRANSFER_ORDER_RECEIVE(9, "领取转账收款"),
    TRANSFER_ORDER_REFUND(10, "转账退款"),

    ;

    private final Integer code;
    private final String msg;

    TransactionTypeEnum(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    // 初始化 code -> msg 映射
    private static final Map<Integer, String> CODE_TO_MSG_MAP = new HashMap<>();
    // 初始化 code -> Enum 映射
    private static final Map<Integer, TransactionTypeEnum> CODE_TO_ENUM_MAP = new HashMap<>();

    static {
        for (TransactionTypeEnum typeEnum : TransactionTypeEnum.values()) {
            CODE_TO_MSG_MAP.put(typeEnum.getCode(), typeEnum.getMsg());
            CODE_TO_ENUM_MAP.put(typeEnum.getCode(), typeEnum);
        }
    }

    public static String getMsgByCode(Integer code) {
        if (Objects.isNull(code)) {
            return "未知";
        }
        return CODE_TO_MSG_MAP.getOrDefault(code, "未知");
    }

    public static TransactionTypeEnum getByCode(Integer code) {
        if (Objects.isNull(code)) {
            return null;
        }
        return CODE_TO_ENUM_MAP.get(code);
    }

}
